package com.meb.imageexif

import android.os.Bundle
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.LocationSource
import com.amap.api.maps2d.UiSettings
import com.amap.api.maps2d.model.CameraPosition
import com.amap.api.maps2d.model.LatLng
import com.amap.api.maps2d.model.MyLocationStyle

import com.meb.imageexif.event.MapEvent
import com.sugar.sugarlibrary.base.BaseActivity
import com.sugar.sugarlibrary.rx.RxEventBus
import kotlinx.android.synthetic.main.activity_map.*

class MyMapActivity : BaseActivity(), LocationSource, AMap.OnMapClickListener {
    private var aMap: AMap? = null
    private var mUiSettings: UiSettings? = null
    private var mListener: LocationSource.OnLocationChangedListener? = null
    private var mLocationClient: AMapLocationClient? = null
    private var mLocationOption: AMapLocationClientOption? = null
    private var mMapCenterLatLng: LatLng? = null  //保存地图中心点坐标
    private var clickLatLng: LatLng? = null
    override fun init(savedInstanceState: Bundle?) {
        iv_map_left_back.setOnClickListener {
            onBackPressed()
        }
        tv_ok.setOnClickListener {
            if (clickLatLng != null){
                RxEventBus.getInstance().post(MapEvent(clickLatLng!!))
                finish()
            }

        }
            mapViews.onCreate(savedInstanceState)
            initMap()
    }




    private fun initMap() {
        aMap = mapViews.map
        aMap?.apply {
            mUiSettings = uiSettings
            mUiSettings?.apply {
                isZoomControlsEnabled = false   //不显示默认缩放控制按钮
                isCompassEnabled = true         //指南针是否可见
                isScaleControlsEnabled = false  //比例尺不可见
            }
            setOnMapClickListener(this@MyMapActivity)
            animateCamera(CameraUpdateFactory.newLatLngZoom(cameraPosition.target, 14f), 150L, object : AMap.CancelableCallback {
                override fun onFinish() {
                }

                override fun onCancel() {
                }
            })




            setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
                override fun onCameraChangeFinish(cameraPosition: CameraPosition) {
                    mMapCenterLatLng = cameraPosition.target
                }

                override fun onCameraChange(cameraPosition: CameraPosition) {
//                    logd("onCameraChange: ${cameraPosition.target}")

                }
            })
            initMapLocation()
        }



    }

    private fun initMapLocation() {
        aMap?.apply {
            setLocationSource(this@MyMapActivity) // 设置定位监听
            isMyLocationEnabled = true // 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
            setMyLocationStyle(MyLocationStyle().also {
                it.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE)
                it.showMyLocation(true)//设置是否显示定位小蓝点，用于满足只想使用定位，不想使用定位小蓝点的场景，设置false以后图面上不再有定位蓝点的概念，但是会持续回调位置信息。
                //                it.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.main_map_center_icon))
            })

        }
    }

    override fun loadData() {
    }

    override fun getContentView(): Int {
        return R.layout.activity_map
    }

    override fun deactivate() {
        mListener = null
        mLocationClient?.let {
            it.stopLocation()
            it.onDestroy()
        }
        mLocationClient = null
    }

    override fun activate(listener: LocationSource.OnLocationChangedListener?) {
        mListener = listener
        if (null == mLocationClient) {
            mLocationOption = AMapLocationClientOption().also {
                it.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
                it.isOnceLocation = true
            }
            mLocationClient = AMapLocationClient(this).also {
                it.setLocationListener { aMapLocation ->
                    mListener?.apply {
                        if (null != aMapLocation && aMapLocation.errorCode == 0) {
                            this.onLocationChanged(aMapLocation)
                            removeToMapPoint(LatLng(aMapLocation.latitude, aMapLocation.longitude))

                        } else {
                        }
                    }
                }
                it.setLocationOption(mLocationOption)
                it.startLocation()
            }
        }
    }

    /**
     * 移动到地图指定位置
     */
    private fun removeToMapPoint(latLng: LatLng) {
        if (latLng == mMapCenterLatLng) {
            return
        }
        aMap?.apply {
            val currentZoom = cameraPosition.zoom
            animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, currentZoom), 150L, object : AMap.CancelableCallback {
                override fun onFinish() {
                }

                override fun onCancel() {
                }
            })
        }
    }

    override fun onResume() {
        super.onResume()
        mapViews.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapViews.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapViews.onDestroy()
    }

    override fun onMapClick(p0: LatLng?) {
        tv_jwd.text = p0?.toString()
        clickLatLng = p0
        if (p0 != null){
            removeToMapPoint(p0!!)
        }

    }
}